<?php
require('includes/top.php');

foreach($_POST as $key => $value)
{
if($value!="" && $value!="Submit")
{
$data = $key.'_'.$value;

$approved_accessories_array = explode('_',$data);
$approved_id = $approved_accessories_array[1];
$user_id = $approved_accessories_array[2];

$date = date("d-m-y");
$sql="INSERT INTO approved_accessories(`accessories_id`,`user_id`,`date`,`user_name`) VALUES ('$approved_id','$user_id','$date','".$_SESSION['user_name']."')";

$query = mysql_query("SELECT accessories_name,depot_location FROM accessories where user_id = '".trim($user_id)."' and accessories_id = '".trim($approved_id)."'");
$queryresult = mysql_fetch_array($query);

$locquery = mysql_query("SELECT user_email_address FROM users where user_address = '".trim($queryresult['depot_location'])."'");
$locqueryresult = mysql_fetch_array($locquery);


// recipients
$to  = $locqueryresult['user_email_address'];
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Laptrak</title>
</head>
<body>
  <p><strong>Following Accessories are pending to be dispatched:</strong></p>
  <table>
    <tr>
      <td>Accessories :</td><td>'.$queryresult['accessories_name'].'</td>
    </tr>    
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From: no-reply@laptrak.com' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);

$result=mysql_query($sql);

$sql1="UPDATE accessories SET `status` = 'approve' WHERE accessories_id = '".trim($approved_id)."' and user_id = '".trim($user_id)."'";
$result1=mysql_query($sql1);

}
}

if($result>0)
{
header('Location:view_accessories_request.php?v=success');
}
else
{
header('Location:view_accessories_request.php?v=err');
}
?>